exports.__esModule = true;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.Service {
    constructor() {
        super();
        const db = this.db;
        this.postModel = db['sys_post'];
        this.userModel = db['sys_user'];
        this.postUserModel = db['sys_user_post'];
    }

    /**
     * 查询岗位列表信息
     * @data
     * post_name --岗位名称
     * post_code --岗位编码
     * --------以上为条件查询--------
     * options --分页查询条件
     * options.page --页码 0开始
     * opations.pageSize --单页记录数
     * @return
     * flag --标识
     * count --记录总数
     * rows --当前页数据
     */
    getPostList(data) {
        var _this = this;

        return _asyncToGenerator(function* () {
            data.start = (parseInt(data.page) - 1) * parseInt(data.pageSize);
            const Op = _this.db.Sequelize.Op;
            let where = {};
            if (data.post_name) {
                where.post_name = {
                    [Op.like]: '%' + data.post_name + '%'
                };
            }
            try {
                const condition = {
                    where: where,
                    limit: parseInt(data.pageSize),
                    offset: parseInt(data.start),
                    order: [['post_sort', 'asc']]
                };
                return yield _this.postModel.findAndCountAll(condition);
            } catch (err) {
                return _this.exceptionService.handleError(err);
            }
        })();
    }
    /**
     * 保存岗位信息 新增|编辑
     * @data 岗位信息
     * @returns Boolean
     */
    savePostInfo(data) {
        var _this2 = this;

        return _asyncToGenerator(function* () {
            const Op = _this2.db.Sequelize.Op;
            try {
                const oldPost = yield _this2.postModel.findOne({
                    where: {
                        [Op.or]: {
                            post_name: data.post_name,
                            post_code: data.post_code
                        }
                    },
                    attributes: ['id', 'post_name', 'post_code']
                });
                if (data.id) {
                    if (oldPost && oldPost.id !== data.id) {
                        return false;
                    } else {
                        return yield _this2.postModel.update(data, {
                            where: {
                                id: data.id
                            }
                        });
                    }
                } else {
                    if (oldPost) {
                        return false;
                    } else {
                        data.id = think.uuid('v1');
                        return yield _this2.postModel.create(data);
                    }
                }
            } catch (err) {
                return _this2.exceptionService.handleError(err);
            }
        })();
    }

    /**
     * 删除岗位信息
     * @data
     * data.id --删除岗位id
     */
    delPostInfo(data) {
        var _this3 = this;

        return _asyncToGenerator(function* () {
            const Op = _this3.db.Sequelize.Op;
            const where = { id: data.id };
            try {
                return yield _this3.postModel.destroy({ where: where });
            } catch (err) {
                return _this3.exceptionService.handleError(err);
            }
        })();
    }

    getAllPost() {
        var _this4 = this;

        return _asyncToGenerator(function* () {
            try {
                return yield _this4.postModel.findAll({
                    attributes: ['id', 'post_code', 'post_name'],
                    order: [['post_sort', 'asc']]
                });
            } catch (err) {
                return _this4.exceptionService.handleError(err);
            }
        })();
    }

    saveUserPost(data) {
        var _this5 = this;

        return _asyncToGenerator(function* () {
            const Op = _this5.db.Sequelize.Op;
            try {
                if (data.delList.length > 0) {
                    yield _this5.postUserModel.destroy({ where: { user_id: data.user_id } });
                }
                if (data.addList.length > 0) {
                    const addData = data.addList.map(function (item) {
                        const param = {
                            user_id: data.user_id,
                            post_id: item
                        };
                        return param;
                    });
                    yield _this5.postUserModel.bulkCreate(addData);
                }
                return true;
            } catch (err) {
                return _this5.exceptionService.handleError(err);
            }
        })();
    }

};